#include "widget.h"
#include "ui_widget.h"
#include <QDebug>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    //设置随机数种子
    srand(0);
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_down_clicked()
{
    //获取当前位置
    QRect rect = ui->pushButton_target->geometry();
    qDebug() <<rect;
    //这样只能修改按钮左上角的位置
    rect.setY(rect.y()+10);
    ui->pushButton_target->setGeometry(rect);
}

void Widget::on_pushButton_up_clicked()
{
    //获取当前位置
    QRect rect = ui->pushButton_target->geometry();
    qDebug() <<rect;
    //左上角上移后，再改变宽度，就可以实现整合上移
    rect.setY(rect.y()-10);
    rect.setHeight(rect.height()-10);
    ui->pushButton_target->setGeometry(rect);
}

void Widget::on_pushButton_left_clicked()
{
    //获取当前位置
    QRect rect = ui->pushButton_target->geometry();
    qDebug() <<rect;
    //也可用setGeometry的第二个重载，直接重新设置四个参数
    ui->pushButton_target->setGeometry(rect.x()-10,rect.y(),rect.width(),rect.height());
}



void Widget::on_pushButton_right_clicked()
{
    //获取当前位置
    QRect rect = ui->pushButton_target->geometry();
    qDebug() <<rect;
    rect.setX(rect.x()+10);
    ui->pushButton_target->setGeometry(rect);
}

void Widget::on_pushButton_yes_clicked()
{
    ui->label->setText("yesssss!");
}

void Widget::on_pushButton_no_clicked()
{
    //ui->label->setText("noooooo!");

    //点击后将按钮挪走
    //通过生成随机数实现
    int width=this->geometry().width();
    int height=this->geometry().height();

    int x=rand()%width;
    int y=rand()%height;

    ui->pushButton_no->move(x,y);
}

void Widget::on_pushButton_no_pressed()
{
    //鼠标按下后按钮挪走
    //通过生成随机数实现
    int width=this->geometry().width();
    int height=this->geometry().height();

    int x=rand()%width;
    int y=rand()%height;

    ui->pushButton_no->move(x,y);
}
